<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card class="box-card">
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first" class="tab-pane">
            <el-row>
              <el-button type="primary" style="margin-bottom:20px" @click="addBtnFn"> + 新增角色</el-button>
            </el-row>
            <el-table
              style="width: 100%"
              border
              :data="rolesList"
            >
              <el-table-column
                type="index"
                label="序号"
                width="180"
              />
              <el-table-column
                prop="name"
                label="角色名"
                width="180"
              />
              <el-table-column
                prop="description"
                label="描述"
              />
              <el-table-column
                label="操作"
                width="280"
              >
                <template slot-scope="scope">
                  <el-button type="success" size="small" @click="handleClick(scope.row.id)">分配权限</el-button>
                  <el-button type="primary" size="small" @click="editClick(scope.row)">编辑</el-button>
                  <el-button type="danger" size="small" @click="removeClick(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              :current-page="params.page"
              :page-sizes="[10, 15, 20, 25]"
              :page-size="params.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
            <el-dialog
              :title="isEdit?'编辑角色':'添加角色'"
              :visible.sync="dialogFormVisible"
              :close-on-click-modal="false"
              :close-on-press-escape="false"
              @close="cancelFn"
            >
              <el-form ref="rolesForm" :model="roleForm" label-width="100px" :rules="rules">
                <el-form-item label="角色名称" prop="name">
                  <el-input v-model="roleForm.name" autocomplete="off" />
                </el-form-item>
                <el-form-item label="角色描述" prop="description">
                  <el-input v-model="roleForm.description" autocomplete="off" />
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="cancelFn">取 消</el-button>
                <el-button type="primary" @click="addRolesFn">确 定</el-button>
              </div>
            </el-dialog>
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">
            <el-alert
              title="消息提示的文案"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="80px" :model="companyInfo">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.name" disabled />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.companyAddress" disabled />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyInfo.remarks" disabled />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
        <!-- 分配角色弹框 -->
        <el-dialog
          title="提示"
          :visible.sync="assignDialognVisible"
          width="50%"
        >
          <assign-permission v-if="assignDialognVisible" :role-id="roleId" @close="closeHandle" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import assignPermission from './assignPermission.vue'
import { getRolesAPI, getCompanyInfoAPI, addRolesAPI, getRolesByIdAPI, updateRolesByIdAPI, removeRolesByIdAPI } from '@/api/setting'
export default {
  name: 'Setting',
  components: {
    assignPermission
  },
  data() {
    return {
      activeName: 'first', // 选中的导航
      params: { // 获取角色列表传参
        page: 1, // 当前页码
        pagesize: 10// 每页显示条数
      },
      total: 0, // 列表总条数
      rolesList: [], // 角色列表数据
      companyInfo: {}, // 公司信息
      dialogFormVisible: false, // 控制弹窗的显示与否
      roleForm: {
        name: '',
        description: ''
      }, // 新增角色表单
      rules: { // 表单校验规则
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      },
      isEdit: false, // 是否为编辑状态
      roleId: '', // 当前角色id
      assignDialognVisible: false// 权限分配弹框是否显示
    }
  },
  created() {
    this.getRolesList()
    this.getCompanyInfo()
  },
  methods: {
    // 获取公司信息
    async getCompanyInfo() {
      const id = this.$store.getters.companyId
      const res = await getCompanyInfoAPI(id)
      this.companyInfo = res.data
    },
    // 点击添加角色按钮,触发事件
    addBtnFn() {
      this.dialogFormVisible = true
    },
    // 点击分配权限按钮
    handleClick(id) {
      // 出现弹窗
      this.assignDialognVisible = true
      this.roleId = id
    },
    // 获取所有角色列表数据
    async getRolesList() {
      // 调用接口
      const res = await getRolesAPI(this.params)
      // 把角色列表数据赋值给变量
      this.rolesList = res.data.rows
      this.total = res.data.total
    },
    // 点击编辑按钮触发事件
    async editClick(data) {
      // 获取角色信息,数据回填
      this.isEdit = true
      this.dialogFormVisible = true
      const res = await getRolesByIdAPI(data.id)
      this.roleForm = res.data
    },
    // 点击删除按钮,触发事件
    async removeClick(data) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        const id = data.id
        await removeRolesByIdAPI(id)
        if (this.rolesList.length === 1) {
          this.params.page--
        }
        this.getRolesList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 每页显示条数发生改变触发函数
    handleSizeChange(pagesize) {
      this.params.pagesize = pagesize
      this.getRolesList()
    },
    // 页码发生改变,触发函数
    handleCurrentChange(page) {
      this.params.page = page
      this.getRolesList()
    },
    // 表单确定按钮事件
    addRolesFn() {
      this.$refs.rolesForm.validate(async bool => {
        if (!bool) return
        if (!this.isEdit) {
          await addRolesAPI(this.roleForm)
        } else {
          const res = await updateRolesByIdAPI(this.roleForm.id, this.roleForm)
          console.log(this.roleForm, res)
        }
        this.getRolesList()
        this.dialogFormVisible = false
        this.$refs.rolesForm.resetFields()
      })
    },
    // 点击表单取消,触发事件
    cancelFn() {
      this.dialogFormVisible = false
      this.$refs.rolesForm.resetFields()
    },
    // 关闭弹窗触发事件
    closeHandle() {
      this.assignDialognVisible = false
    }
  }

}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}
.tab-pane {
  padding: 20px;
}
.el-pagination {
  text-align: center;
  margin-top: 20px;
}
.el-form{
  margin-top:50px
}
</style>
